package com.lwh.easyExcel.service.impl;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.lwh.common.uniqueuekey.IdWorker;
import com.lwh.easyExcel.controller.BdCheliangController;
import com.lwh.easyExcel.dao.BdCheliangMapper;
import com.lwh.easyExcel.entity.BdCheliangEntity;
import com.lwh.easyExcel.excelModel.CheliangListener;
import com.lwh.easyExcel.service.BdCheliangService;
import com.lwh.common.util.DateUtil;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.util.*;


@Service
public class BdCheliangServiceImpl implements BdCheliangService {

    @Autowired
    private BdCheliangMapper bdCheliangMapper;



    @Override
    public Map<String, Object> findPage(BdCheliangEntity query)  {
        int total = bdCheliangMapper.findPageCount(query);
        List<BdCheliangEntity> list = bdCheliangMapper.findPage(query);
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("total", total);
        map.put("rows", list);
        return map;

    }



    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int insertSelective(BdCheliangEntity bdCheliangEntity) {
        IdWorker idWorker=new IdWorker(0,0);
        String id = String.valueOf(idWorker.nextId());
        bdCheliangEntity.setId(id);
        bdCheliangEntity.setCreateDate(new Date());
        bdCheliangMapper.insertSelective(bdCheliangEntity);

        return 1;
    }

    @Override
    public int updateByPrimaryKeySelective(BdCheliangEntity bdCheliangEntity) {
        return bdCheliangMapper.updateByPrimaryKeySelective(bdCheliangEntity);
    }

    @Override
    public int deleteByIds(String[] ids) {
        return bdCheliangMapper.deleteMore(ids);
    }

    @Override
    public int deleteOneById(String id) {
        return bdCheliangMapper.deleteOne(id);
    }



    /*@Override
    public Map<String, Object> outExcel(BdCheliangEntity bdCheliangEntity) {

        List<BdCheliangEntity> list = bdCheliangMapper.outExcel(bdCheliangEntity);
        ExcelWriter writer = null;
        OutputStream out = null;
        Map<String, Object> rsultMap = new HashMap<String, Object>();

        String fileName = "车辆管理";
        String dateStr = DateUtil.dateToString(new Date(), "yyyyMMddhhmmss");
        fileName = fileName + dateStr + ".xlsx";
        try {
            // 封装路径
            String filePath = BdCheliangController.class.getResource("").toURI().getPath();
            System.out.println(filePath);
            filePath = filePath.substring(0, filePath.lastIndexOf("/com")) + "/static/excel/" + fileName;

            File file = new File(filePath);
            if (!file.isFile()) {
                file.createNewFile();
            }
            out = new FileOutputStream(file);
            writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            Sheet sheet = new Sheet(1, 0,BdCheliangEntity.class);
            sheet.setSheetName(fileName);
            writer.write(list, sheet);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            rsultMap.put("success", false);
            return rsultMap;
        } finally {
            if (writer != null) {
                writer.finish();
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        rsultMap.put("success", true);
        rsultMap.put("url", fileName);
        return rsultMap;

    }*/

    @Override
    public List<BdCheliangEntity> outExcel(BdCheliangEntity bdCheliangEntity) {
        List<BdCheliangEntity> list=bdCheliangMapper.outExcel(bdCheliangEntity);
        return list;
    }

    @Override
    public List<BdCheliangEntity> findAll() throws Exception {
        return null;
    }

    // 导入
    @Override
    public String readExcel(InputStream is) {
        IdWorker idWorker=new IdWorker(0,0);
        ExcelReader excelReader = null;

        // 实例化实现了AnalysisEventListener接口的类
        CheliangListener listener = new CheliangListener();

        // 实例化 ExcelReader，传入参数
        // 有两种方法，一种是实例ExcelReader构造方法，一种是调用EasyExcelFactory.getReader
        //  excelReader = new ExcelReader(is, ExcelTypeEnum.XLSX, null, listener);
        excelReader = EasyExcelFactory.getReader(is, listener);

        // 读取信息
        excelReader.read(new Sheet(1, 1, BdCheliangEntity.class));

        List<Object> importList = listener.getDatas();
        List<BdCheliangEntity> list = new ArrayList<>();

        BdCheliangEntity info = new BdCheliangEntity();

        // 转换数据类型,并插入到数据库
        for (int i = 0; i < importList.size(); i++) {
            info = (BdCheliangEntity) importList.get(i);
            String id = String.valueOf(idWorker.nextId());
            info.setId(id);
            info.setCreateDate(new Date());
            list.add(info);

        }
        if(list.size()>=1) {
            bdCheliangMapper.importExcel(list);
            list.clear();
        }

        return "success";
    }


}
